package com.ruoyi.crm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.MongoLog;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.domain.vo.BusinessTrackRecordVo;
import com.ruoyi.crm.domain.vo.ContractVo;
import com.ruoyi.crm.service.IBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/crm/business/track")
public class BusinessTrackRecordController extends BaseController {
    @Autowired
    private IBusinessService businessService;

    /**
     * 新增线索跟进记录
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @Log(title = "商机跟进记录", businessType = BusinessType.INSERT)
    @MongoLog(tableName = MongoLog.TableName.BUSINESS, title = "商机跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult record(@RequestBody BusinessTrackRecordVo businessTrackRecordVo) {
        businessTrackRecordVo.setStatus(Clue.Status.TRACKING.getValue());
        return toAjax(businessService.updateBusiness(businessTrackRecordVo));
    }

    /**
     * 踢回公海
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @Log(title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @MongoLog(tableName = MongoLog.TableName.BUSINESS, title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/giveBack/{id}")
    public AjaxResult giveBack(@RequestBody Map<String, Object> body) {
        return toAjax(businessService.giveBack(Long.valueOf("" + body.get("id"))));
    }

    /**
     * 捞取
     * @param body
     * @return
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @Log(title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @MongoLog(tableName = MongoLog.TableName.BUSINESS, title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/reset/{id}")
    public AjaxResult reset(@RequestBody Map<String, String> body)
    {
        return toAjax(businessService.reset(Long.valueOf(body.get("id"))));
    }

    /**
     * 转客户
     * @param contractVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @Log(title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @MongoLog(tableName = MongoLog.TableName.BUSINESS, title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/transform/{id}")
    public AjaxResult transform(@RequestBody ContractVo contractVo)
    {
        return toAjax(businessService.transform2Customer(contractVo));
    }
}
